<template>
  <div :class="{'table-head':true,mt0:path.indexOf('/student/only')>-1}">
    <p class="title box-size border-common">
      <img class="ver-middle" :src="img?imgs[img]:imgs.table" alt="">
      <span class="ver-middle">{{getTitle}}</span>
      <span class="fr box-size right-btn">
        <span :class="item.class?(item.class+' my-btn box-size'):'my-btn box-size'" v-for="item in btnData" v-if="item.show&&!item.isSelect&&!item.isImport" @click="item.click">{{item.label}}</span>
        <!-- <el-select  v-for="(item,index) in btnData" :key="index" v-if="item.show&&item.isSelect" v-model="item.value" placeholder="请选择"><el-option v-for="one in 15" :key="one" :value="one">{{one}}</el-option></el-select> -->
        <el-upload
          v-else-if="item.isImport&&item.show"
          class="import-btn fl"
          :action="formaltUrl"
          :on-success="handleChange"
          :multiple="false"
          :show-file-list="false"
          accept=".xls,.xlsx,.xlsm,.xlam"
          >
          <span :class="item.class?(item.class+' my-btn box-size'):'my-btn box-size'">{{item.label}}</span>
        </el-upload>
      </span>
    </p>

    <p style="display:none">{{getBtn}}</p>
  </div>
</template>

<script>

export default {
  props:["img","btns","title","table","search","index"],
  data() {
    return {
      btnData:[
        {
          label:"保存编辑",
          show:true,
          class:"bac-blue",
          showStr:"1",
          click:this.save
        },
        {
          label:"查询结果",
          show:true,
          class:"bac-blue",
          showStr:"1-1-1,2-1",
          click:this.query
        },{
          label:"导入数据",
          show:true,
          class:"bac-green",
          showStr:"1-1-2,2-3,2-3-1",
          isImport:true,
          importUrl:"import_1",
          click:this.importData
        },{
            label:"字典管理",
            show:true,
            class:"bac-green",
            showStr:"2-3",
            click:this.fields
          },{
          label:"添加",
          show:true,
          class:"bac-green",
          showStr:"2-2,2-3,3-1,4-1,2-3-1",
          click:this.add
        },{
          label:"导出数据",
          show:true,
          class:"bac-gold",
          showStr:"1-1-2,2-2,2-3,2-3-1",
          click:this.export
        },{
          label:"编辑",
          show:true,
          class:"bac-green",
          showStr:"2-5",
          click:this.edit
        },{
          label:"删除",
          show:true,
          class:"bac-red",
          showStr:"2-5",
          click:this.delete
        },{
          label:"返回",
          show:true,
          class:"bac-green",
          showStr:"2-5",
          click:this.doback
        },{
          label:"保存",
          show:true,
          class:"bac-green",
          showStr:"2-6",
          click:this.saveStu
        },{
          label:"取消",
          show:true,
          class:"bac-gray",
          showStr:"2-6",
          click:this.cancel
        }

      ],

      imgs:{
        table:require("../../assets/img/table.png"),
        search:require("../../assets/img/search.png")
      },

      url:"",
      urls:{
        router:null,//当前路由
        import_1:PATH+"upload/uploadFile",//学生管理批量的url
      },
      path:"",

      testArray:[
        "/academicpaper",
        "/teachmaterial",
        "/scientificawards",
        "/softwork",
        "/joinactivity",
        "/runmeeting",
        "/competition",
        "/guidance",
        "/otherteaching",
        "/famoust",
        "/classChange",
        "/researchs",
        "/teachingpaper",
        "/textbooks",
        "/honorary",
        "/professional",
        "/teaching/results",

        "/teaching/project",
        "/teaching/appraise",
        "/teaching/reform",
        "/teaching/course"
      ]
    };
  },
  computed:{
    formaltUrl(){
      var path=_g.getRouterPath(this)
      var url="index/upload"
      switch(path){
        case "/student":url="upload/uploadFile";break;
      }
      return PATH+url
    },
    getTitle(){
      return this.title||"筛选查询"
    },
    getBtn(){
      this.btnData.forEach(ele=>{
        if(ele.showStr.split(",").includes(this.btns)){
          ele.show=true
        }else{
          ele.show=false
        }
      })
    }
  },
  methods:{
    // 判断显示的按钮
    btnsShow(){
      this.btnData.forEach(ele=>{
        if(ele.showStr.split(",").includes(this.btns)){
          ele.show=true
        }else{
          ele.show=false
        }
      })
    },

    query(){
      bus.$emit("flashSocialTable")
    },

    // 保存
    save(){
      bus.$emit("baseInfoSaved")
    },

    add(){
      var path=_g.getRouterPath(this)
      
      if(path=="/student"){
        this.$router.push("/student/add")
        return
      }else if(path=="/student/only"){
        this.$router.push("/student/only/add")
        return
      }else if(path=="/center"){
        this.$store.state.dia.center.index=this.index
        this.$store.state.dia.center.title="新增"
        this.$store.state.dia.center.show=true
        return
      }else if(path=="/privilege"){
        this.$store.state.dia.privilege.title="新增"
        this.$store.state.dia.privilege.show=true
        this.$store.state.dia.privilege.isAdd=true
        return
      }else if(this.testArray.includes(path)){
        this.$store.state.dia.academic.show=true
        return 
      }

      this.$store.state.dia.add.id=null
      this.$store.state.dia.add.tableName=this.table
      var url="" //设置新增的地址
      if(this.urls.router.indexOf("/socailServe/")>-1){
        url="teacher_manage/infoEdit";
        this.$store.state.dia.socialInfo.show=true;
      }else if(this.urls.router.indexOf("/party/")>-1){
        url="teacher_manage/infoEdit";
        var arrays=this.$store.state.urlArray
        for(var i=0;i<arrays.length;i++){
          if(this.urls.router==arrays[i]){
            this.$store.state.dia.add.type=i+1
            break;
          }
        }
        this.$store.state.dia.add.show=true;
      }
      this.$store.state.dia.add.addUrl=url
    },
      fields(){
          this.$store.state.dia.fields.show=true
          return


          this.$store.state.dia.add.id=null
          this.$store.state.dia.add.tableName=this.table
          var url="" //设置新增的地址
          if(this.urls.router.indexOf("/socailServe/")>-1){
              url="teacher_manage/infoEdit";
              this.$store.state.dia.socialInfo.show=true;
          }else if(this.urls.router.indexOf("/party/")>-1){
              url="teacher_manage/infoEdit";
              var arrays=this.$store.state.urlArray
              for(var i=0;i<arrays.length;i++){
                  if(this.urls.router==arrays[i]){
                      this.$store.state.dia.add.type=i+1
                      break;
                  }
              }
              this.$store.state.dia.add.show=true;
          }
          this.$store.state.dia.add.addUrl=url
      },
    // 导出
    export(){
      var path=_g.getRouterPath(this)
      var params={}
      var url=""
      this.$store.state.dia.export.OK=null
      this.$store.state.dia.export.params=[]
      switch(path){
        case "/student":this.$store.state.dia.export.show=true;this.$store.state.dia.export.search=this.search;return;
        case "/teacherManage/paper":url="teacher/academicexport";break;
        case "/teacherManage/scientific":url="teacher/worksexport";break;
        case "/teacherManage/meetings":url="teacher/activitysexport";break;
        case "/teacherManage/communicate":url="teacher/meetingexport";break;
        case "/teacherManage/info":url="teacherinfo/export";break;
        // case "/academicpaper":url="teacher/exportall";params=this.search;params.tablename="outcomes_academic";break;
        // case "/teachmaterial":url="teacher/exportall";params=this.search;params.tablename="outcomes_monograph";break;
        // case "/scientificawards":url="teacher/exportall";params=this.search;params.tablename="outcomes_scientific";break;
        // case "/softwork":url="teacher/exportall";params=this.search;params.tablename="outcomes_patent";break;
        // case "/joinactivity":url="teacher/exportall";params=this.search;params.tablename="academic_activity";break;
        // case "/runmeeting":url="teacher/exportall";params=this.search;params.tablename="academic_meeting";break;
        // case "/competition":url="teacher/exportall";params=this.search;params.tablename="graduate_award";break;
      }
      if(this.testArray.includes(path)){
        this.$store.state.dia.export.show=true;
        this.$store.state.dia.export.search=this.search;
        this.$store.state.dia.export.OK=this.exportFun
        return;

        // url="teacher/exportall"
        // this.$store.state.commonMenu.forEach(ele => {
        //   if(ele.children){
        //     params.tablename=ele.children.find(val=>val.url==path)?ele.children.find(val=>val.url==path).tablename:""
        //   }else if(ele.url===path){
        //     params.tablename=ele.tablename
        //   }
        // });
        // params.tablename=this.$store.state.commonMenu.find(val=>val.url==path).tablename
      }

      _g.apiPost(url,params).then(res=>{
        _g.toastMsg(res.code==0?"success":"error",res.msg)
        if(res.code==0)location.href=res.data
      })
    },

    /**
     * @name 导出
     */
    exportFun(){
      var path=_g.getRouterPath(this)
      var url="teacher/exportall"
      var params={
        expCellName:this.$store.state.dia.export.params,
      }
      this.$store.state.commonMenu.forEach(ele => {
        if(ele.children&&ele.children.find(val=>val.url==path)){
          params.tablename=ele.children.find(val=>val.url==path)?(ele.children.find(val=>val.url==path).tablename):(params.tablename||"")
        }else if(ele.url===path){
          params.tablename=ele.tablename
        }
      });
      _g.apiPost(url,params).then(res=>{
        _g.toastMsg(res.code==0?"success":"error",res.msg)
        if(res.code==0)location.href=res.data
      })
    },

    // 导入
    importData(){
      var path=_g.getRouterPath(this)
      
      console.log("click")
    },
    handleChange(e){
      var path=_g.getRouterPath(this)
      if(path=="/student"){
        if(!e.error){
          this.apiPost("studentinfo/import",{url:e.msg}).then(res=>{
            _g.toastMsg(res.error?"error":"success",res.msg)
            bus.$emit("flash","flash")
            if(res.error){
              location.href=res.data
            }
          })
        }
      }else{
        var url=""
        var params = {url:e.data}
        switch(path){
          case "/teacherManage/paper":url="teacher/academicimport";break;
          case "/teacherManage/scientific":url="teacher/worksimport";break;
          case "/teacherManage/meetings":url="teacher/activitysimport";break;
          case "/teacherManage/communicate":url="teacher/meetingimport";break;
          case "/teacherManage/info":url="teacherinfo/import";break;
          // case "/academicpaper":url="teacher/importall";params.tablename="outcomes_academic";break;
          // case "/teachmaterial":url="teacher/importall";params.tablename="outcomes_monograph";break;
          // case "/scientificawards":url="teacher/importall";params.tablename="outcomes_scientific";break;
          // case "/softwork":url="teacher/importall";params.tablename="outcomes_patent";break;
          // case "/joinactivity":url="teacher/importall";params.tablename="academic_activity";break;
          // case "/runmeeting":url="teacher/importall";params.tablename="academic_meeting";break;
          // case "/competition":url="teacher/importall";params.tablename="graduate_award";break;
        }
        if(this.testArray.includes(path)){
          url="teacher/importall"
          this.$store.state.commonMenu.forEach(ele => {
            if(ele.children){
              params.tablename=ele.children.find(val=>val.url==path)?ele.children.find(val=>val.url==path).tablename:(params.tablename||"")
            }else if(ele.url===path){
              params.tablename=ele.tablename
            }
          });
          // params.tablename=this.$store.state.commonMenu.find(val=>val.url==path).tablename
        }
        _g.apiPost(url,params).then(res=>{
          _g.toastMsg(res.code?"error":"success",res.msg)
          if(!res.code){
            bus.$emit("import_flash")
            bus.$emit("flash")
          }
        })
      }
      
      
    },
    delete(){
      bus.$emit("saveStudent","delete")
    },

    edit(){
      bus.$emit("saveStudent","edit")
    },
    doback(){
      if(this.path.indexOf('/student/only')>-1){
        this.$router.push("/student/only")
      }else if(this.path=="/student/add"||this.path.indexOf('/student/detail/')>-1){
        this.$router.push("/student")
      }
    },

    // 保存
    saveStu(){
      bus.$emit("saveStudent","save")
    },
    cancel(){
      bus.$emit("saveStudent","cancel")
    }
  },
  components: {},
  created() {
    this.urls.router=_g.getRouterPath(this)
    this.btnsShow()
  },
  mounted(){
    this.path=_g.getRouterPath(this)
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.mt0{
  margin-top: 0!important;
}
p {
  padding: 0;
  height: 46px;
  line-height: 45px;
  text-align: left;
  color: #666;
  font-size: 12px;
  background-color: #f0f0f0;
  /* border-bottom: none; */
}

.title > img {
  display: inline-block;
  width: 20px;
  margin: 0px 10px;
}
.right-btn {
  height: 45px;
  line-height: 45px;
  position: relative;
}
.my-btn {
  display: inline-block;
  text-align: center;
  margin-right: 10px;
  line-height: 30px;
  width: 80px;
  height: 30px;
  margin-top: 7.5px;
  cursor: pointer;
}
.title .my-btn:last-child {
  margin-right: 20px;
}

.el-input{
  width: 90px;
}
.extend-class{
  color: #0099FF;
  margin-right: 10px;
  cursor: pointer;
}
.extend-class:hover{
  text-decoration: underline;
}
.bac-blue{
  background-color: rgba(0, 153, 255, 1);
  color: #fff;
}
.bac-gold{
  background-color: rgba(255, 153, 0, 1);
  color: #fff;
}
.bac-gray{
  background-color: gray;
  color: #fff;
}
.bac-green{
  background-color: rgba(0, 204, 102, 1);
  color: #fff;;
}
.bac-red{
  background-color: red;
  color: #fff;;
}
</style>
